package features.advance.edu.JPA;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import java.util.List;

/**
 * @author LIN
 * @date 2022-01-13 16:44
 */

public class Main {

    public static void main(String[] args) {
        EntityManagerFactory emf = Persistence
                .createEntityManagerFactory("EmployeeService");
        EntityManager em = emf.createEntityManager();
        EmployeeService service = new EmployeeService(em);

        em.getTransaction().begin();
        Employee emp = service.createEmployee(1, "Tom", 5000);
        em.getTransaction().commit();
        System.out.println("Persisted " + emp);

        emp = service.findEmployee(1);
        System.out.println("Found " + emp);

        List<Employee> emps = service.findAllEmployees();
        for (Employee e : emps)
            System.out.println("Found employee: " + e);

        em.getTransaction().begin();
        emp = service.raiseEmployeeSalary(1, 1000);
        em.getTransaction().commit();
        System.out.println("Updated " + emp);

        em.getTransaction().begin();
        service.removeEmployee(158);
        em.getTransaction().commit();
        System.out.println("Removed Employee 158");

        em.close();
        emf.close();
    }
}
